package com.briup.corejava.day13;

import com.briup.corejava.day10.pojo.Student;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * TreeSet的比较器排序
 * Comparator
 * 实现：compare(T o1, T o2)
 * 比较规则和自然排序是一样的
 * 方法返回正数、负数和0
 */
public class Test1 {
//    class MyComparator implements Comparator<Student>{
//
//        @Override
//        public int compare(Student o1, Student o2) {
//            // 使用年龄降序排序
//            return o2.getAge()-o1.getAge();
//        }
//    }
    public static void main(String[] args) {
        // Comparator comparator = new Test1().new MyComparator();
        Set<Student> set = new TreeSet<>(new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });
        set.add(new Student("zs",19));
        set.add(new Student("zs",20));
        set.add(new Student("ls",22));
        set.add(new Student("ww",21));
        System.out.println(set);
    }
}

